<template>
  <div class="login_container">
    <div class="login_box">
      <el-form label-width="0px" class="login_in" ref="loginFormRef" :model="loginForm" :rules="rules">
        <el-form-item prop="userName">
          <el-input v-model=loginForm.userName type="text" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input v-model=loginForm.passWord type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
          <el-input type="password" v-model="loginForm.checkPass" autocomplete="off" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm()">确认注册</el-button>

        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Zhuce",

  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.loginForm.passWord) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        userName: '',
        passWord: '',
        checkPass: ''
      },
      rules: {
        userName: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 8, message: '长度在3到8个字符', trigger: 'blur'}
        ],
        passWord: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 5, max: 16, message: '长度在5到16个字符', trigger: 'blur'}
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      let config = {
        // 关于comnteng-type，看https://www.jb51.net/article/145209.htm
        headers: {
          //'Content-Type': 'application/json'  //默认是这个
          // 'Content-Type': 'multipart/form-data'
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }
    let data =  {username: this.loginForm.userName, password: this.loginForm.passWord};
      this.$axios.post(
        'http://127.0.0.1:8000/api/register/',data, config).then(response => {
        console.log(response.data)
        if(response.data.code==1)
        {
          alert("register successfully!");
          this.$router.push({path:'/Login'})
        }
        else
        {
          alert("username has been registered!");
        }
      }).catch(function (error) { // 请求失败处理
        console.log(error);
      });
      /*
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          return ;
        } else {
          console.log('error submit!!');
          return false;
        }

      });
      */
      
    }
  }
}
</script>

<style scoped>
.login_container {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url('../assets/img/background-1.jpeg');
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: rgba(255, 255, 255, 0.4);
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 70%;
  transform: translate(-50%, -50%);
}

.login_box .avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  padding: 10px;
}

.login_box .avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.btns {
  display: flex;
  justify-content: flex-end;

}

.login_in {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
